1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.util.concurrent;
18
19 import com.google.common.annotations.Beta;
20 import com.google.common.base.Supplier;
21 import com.google.common.base.Throwables;
22
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26
27
28
29
30
31
32
33
34
35
36
37 @Beta
38 public abstract class AbstractIdleService implements Service {
39
40
41 private final Supplier<String> threadNameSupplier = new Supplier<String>() {
42 @Override public String get() {
43 return serviceName() + " " + state();
44 }
45 };
46
47
48 private final Service delegate = new AbstractService() {
49 @Override protected final void doStart() {
50 MoreExecutors.renamingDecorator(executor(), threadNameSupplier)
51 .execute(new Runnable() {
52 @Override public void run() {
53 try {
54 startUp();
55 notifyStarted();
56 } catch (Throwable t) {
57 notifyFailed(t);
58 throw Throwables.propagate(t);
59 }
60 }
61 });
62 }
63
64 @Override protected final void doStop() {
65 MoreExecutors.renamingDecorator(executor(), threadNameSupplier)
66 .execute(new Runnable() {
67 @Override public void run() {
68 try {
69 shutDown();
70 notifyStopped();
71 } catch (Throwable t) {
72 notifyFailed(t);
73 throw Throwables.propagate(t);
74 }
75 }
76 });
77 }
78 };
79
80
81 protected AbstractIdleService() {}
82
83
84 protected abstract void startUp() throws Exception;
85
86
87 protected abstract void shutDown() throws Exception;
88
89
90
91
92
93
94
95
96
97 protected Executor executor() {
98 return new Executor() {
99 @Override public void execute(Runnable command) {
100 MoreExecutors.newThread(threadNameSupplier.get(), command).start();
101 }
102 };
103 }
104
105 @Override public String toString() {
106 return serviceName() + " [" + state() + "]";
107 }
108
109 @Override public final boolean isRunning() {
110 return delegate.isRunning();
111 }
112
113 @Override public final State state() {
114 return delegate.state();
115 }
116
117
118
119
120 @Override public final void addListener(Listener listener, Executor executor) {
121 delegate.addListener(listener, executor);
122 }
123
124
125
126
127 @Override public final Throwable failureCause() {
128 return delegate.failureCause();
129 }
130
131
132
133
134 @Override public final Service startAsync() {
135 delegate.startAsync();
136 return this;
137 }
138
139
140
141
142 @Override public final Service stopAsync() {
143 delegate.stopAsync();
144 return this;
145 }
146
147
148
149
150 @Override public final void awaitRunning() {
151 delegate.awaitRunning();
152 }
153
154
155
156
157 @Override public final void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException {
158 delegate.awaitRunning(timeout, unit);
159 }
160
161
162
163
164 @Override public final void awaitTerminated() {
165 delegate.awaitTerminated();
166 }
167
168
169
170
171 @Override public final void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException {
172 delegate.awaitTerminated(timeout, unit);
173 }
174
175
176
177
178
179
180
181 protected String serviceName() {
182 return getClass().getSimpleName();
183 }
184 }